package org.zxp.thread.daemon;

import org.junit.Test;

/**
 * @program: zxp-thread-test
 * @description: ${description}
 * @author: X-Pacific zhang
 * @create: 2019-05-08 17:03
 **/
public class DaemonTest {

    public static void main(String[] args) throws InterruptedException {
        //用户线程，当main方法运行完成后，不会退出（junit @Test会直接退出）
        Thread userThread = new Thread(() -> {
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("我是用户线程");
            }
        });
        userThread.start();
        //守护线程，当main方法运行完成后，不会退出
        Thread daemonThread = new Thread(() -> {
            while(true){
                System.out.println("我是守护线程");
            }
        });
        daemonThread.setDaemon(true);
        daemonThread.start();

        //休眠3秒后停止用户线程，不停止守护线程
        Thread.sleep(3000L);
        userThread.interrupt();
    }


    public void test1(){

    }
}
